package com.nintendo.bubblechat.interceptor;

import com.nintendo.bubblechat.exceptions.GraceException;
import com.nintendo.bubblechat.grace.result.ResponseStatusEnum;
import com.nintendo.bubblechat.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisOperator redisOperator;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        String phone = request.getHeader("phone");
        String preToken = redisOperator.get(phone);
        if(token.equals(preToken)){
            return true;
        }else{
            GraceException.display(ResponseStatusEnum.TOKEN_ERROR);
        }
        return token != null;
    }

}
